<template>
  <HeaderPart>我的订单</HeaderPart>
  <div class="wrapper">
    <div class="noPay">
      <p>
        未支付订单信息:
      </p>
      <ul>
        <li
            v-for="item in noPayList"
            :key="item.id"
        >
          <div class="fonts">
            <div>商家信息:{{item.shopId}}</div>
            <div>总共的钱:{{item.orderTotal}}元</div>
            <div>购买日期:{{item.orderDate}}</div>
            <div>订单状态:{{status(item.orderState)}}</div>
            <div>
              <button @click="toPay(item.shopId)" class="cta">
                <span>点击支付</span>
                <svg viewBox="0 0 13 10" height="10px" width="15px">
                  <path d="M1,5 L11,5"></path>
                  <polyline points="8 1 12 5 8 9"></polyline>
                </svg>
              </button>
              <button @click="deleteOrder(item.id, item.shopId)" class="cta">
                <span>删除订单</span>
                <svg viewBox="0 0 13 10" height="10px" width="15px">
                  <path d="M1,5 L11,5"></path>
                  <polyline points="8 1 12 5 8 9"></polyline>
                </svg>
              </button>
            </div>
          </div>
        </li>
      </ul>
    </div>
    <div class="payed">
      <p>已支付订单信息:</p>
      <ul>

        <li
          v-for="item in payedList"
          :key="item.id"
        >
          <div class="fonts">
            <div>商家信息:{{item.shopId}}</div>
            <div>总共的钱:{{item.orderTotal}}元</div>
            <div>购买日期:{{item.orderDate}}</div>
            <div>订单状态:{{status(item.orderState)}}</div>
          </div>
        </li>
      </ul>
    </div>
  </div>
  <TabBar></TabBar>
</template>

<script setup>
import TabBar from "@/components/common/TabBar";
import HeaderPart from "@/components/common/HeaderPart";
import {onBeforeMount, ref} from "vue";
import {getAllOrders, removeOrder, removeOrderDetail} from "@/utils/api/orderManager";
import {status} from '@/mixin/status'
import moment from "moment";
import {useRouter} from "vue-router";
import {removeCart} from "@/utils/api/cartManager";


const noPayList = ref([])
const payedList = ref([])
const userId = JSON.parse(localStorage.getItem("userLoginAll")).id
// 获得所有的订单
onBeforeMount( ()=>{
  getOrderInfo()
})

const getOrderInfo =async ()=>{
  const res =await getAllOrders({userId})
  res.data.map(item=>{
    item.orderDate = moment(new Date(item.orderDate)).format('YYYY-MM-DD hh:mm:ss')
    if(item.orderState === 0){
      noPayList.value.push(item)
    }
    else {
      payedList.value.push(item)
    }
  })
}

// 去支付
const router = useRouter()
const toPay = (shopId)=>{
  router.push("/pay-money/" + shopId)
}

// 删除订单
const deleteOrder = (id, shopId)=>{
  // 删除订单
  removeOrder({id})
  // 删除购物车
  removeCart({userId, shopId})
  // 清空订单详情
  removeOrderDetail({orderId:id})
  // 获取所有订单
  noPayList.value = []
  payedList.value = []
  getOrderInfo()
}

</script>
<style scoped>
.wrapper {
  padding-left: 30px;
  margin-top: 5rem;
  display: flex;
  flex-direction: column;
}
.noPay {

}
.noPay p{
  font-size: 20px;
  font-weight: 700;
}
.noPay ul {
  list-style: none;
}
.payed{

}
.payed p{
  font-size: 20px;
  font-weight: 700;
}
.payed ul {
  list-style: none;
}
ul p {
  color: #e03c3c;
}
.fonts {
  display: flex;
  flex-direction: column;
  margin-bottom: 20px;
}
.cta {
  position: relative;
  margin: auto;
  padding: 12px 18px;
  transition: all 0.2s ease;
  border: none;
  background: none;
}

.cta:before {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  display: block;
  border-radius: 50px;
  background: #b1dae7;
  width: 45px;
  height: 45px;
  transition: all 0.3s ease;
}

.cta span {
  position: relative;
  font-family: "Ubuntu", sans-serif;
  font-size: 18px;
  font-weight: 700;
  letter-spacing: 0.05em;
  color: #234567;
}

.cta svg {
  position: relative;
  top: 0;
  margin-left: 10px;
  fill: none;
  stroke-linecap: round;
  stroke-linejoin: round;
  stroke: #234567;
  stroke-width: 2;
  transform: translateX(-5px);
  transition: all 0.3s ease;
}

.cta:hover:before {
  width: 100%;
  background: #b1dae7;
}

.cta:hover svg {
  transform: translateX(0);
}

.cta:active {
  transform: scale(0.95);
}
</style>